<template>
  <div>
    <!-- banner -->
    <div
      class="home-banner"
      :style="cover"
    >
      <div class="banner-container">
        <!-- 主屏幕标题 -->
        <h1 class="blog-title animated zoomIn">
          {{ currentPageInfo.pageTitle }}
        </h1>
        <!-- 主屏幕副标题 -->
        <div class="blog-intro" v-if="currentPageInfo.pageIntro && currentPageInfo.pageIntro.trim() !== ''">
          {{ currentPageInfo.pageIntro }} <span class="typed-cursor">|</span>
        </div>
      </div>
      <!-- 向下滚动 -->
      <div
        class="scroll-down"
        @click="scrollDown"
      >
        <i class="el-icon-arrow-down scroll-down-effects" />
      </div>
    </div>
    <!-- 主页文章 -->
    <v-row class="home-container">
      <v-col
        md="9"
        cols="12"
      >
        <!--公告通知-->
        <v-card>
          <div style="display: flex;margin-top: 20px;padding: 1rem;height: 60px">
            <div>
              <i
                class="el-icon-message-solid"
                style="color:red;"
              />
            </div>
            <div
              style="margin-left: 10px;width: 90%;
                     overflow: hidden;
                     display: -webkit-box;
                     -webkit-line-clamp: 1;
                     -webkit-box-orient: vertical;
                      cursor: pointer"
              @click="showNotice"
              v-html="noticeContent"
            />
          </div>
        </v-card>
        <v-card
          v-for="(item, index) of articleList"
          :key="item.id"
          class="animated zoomIn article-card"
          style="border-radius: 12px 8px 8px 12px"
        >
          <!-- 文章封面图 -->
          <div :class="isRight(index)">
            <router-link :to="'/articles/' + item.id">
              <v-img
                class="on-hover"
                width="100%"
                height="100%"
                :src="item.headerImg"
              />
            </router-link>
          </div>
          <!-- 文章信息 -->
          <div class="article-wrapper">
            <div style="line-height:1.4">
              <div>
                <router-link :to="'/articles/' + item.id">
                  {{ item.title }}
                </router-link>
                <span
                  v-if="item.browseNum >= 1000 && item.browseNum < 10000"
                  style="font-size:12px;border-radius:3px;border: 1px solid  #f70;text-align: center"
                ><font style="color: #f70;">千次阅读</font></span>
                <span
                  v-if="item.browseNum >= 10000"
                  style="font-size:12px;border-radius:3px;border: 1px solid  #f70;text-align: center"
                ><font style="color: #f70;">万次阅读</font></span>
              </div>
            </div>
            <div class="article-info">
              <!-- 是否置顶 -->
              <span v-if="item.isTop === 1">
                <span style="color:#ff7242">
                  <i class="iconfont iconzhiding" /> 置顶
                </span>
                <span class="separator">|</span>
              </span>
              <!-- 发表时间 -->
              <v-icon size="14">
                mdi-calendar-month-outline
              </v-icon>
              {{ item.createTime | date }}
              <span class="separator">|</span>
              <!-- 文章标签 -->
              <router-link
                style="display:inline-block"
                :to="'/tags/' + item.articleType"
                class="mr-1"
              >
                <i class="el-icon-s-operation" />
                {{ getTagName(item.articleType) }}
              </router-link>
            </div>
            <!-- 文章内容 -->
            <div class="article-content">
              {{ item.introduction }}
            </div>
          </div>
        </v-card>
        <!-- 加载按钮 -->
        <el-row
          type="flex"
          justify="center"
          style="margin-top: 20px"
        >
          <el-col :span="8">
            <v-btn
              v-show="articleLoadState"
              outlined
              style="width: 100%;border: solid 1px #e2e3e5;color: #374151;border-radius: 5px"
              :loading="loading"
              @click="loadArticle"
            >
              {{ loadBtnText }}
            </v-btn>
            <el-divider v-if="!articleLoadState">
              <span>我也是有底线的</span>
            </el-divider>
          </el-col>
        </el-row>
        <!-- 无限加载 -->
        <!--        <infinite-loading @infinite="loadArticle">-->
        <!--          <div slot="no-more" />-->
        <!--        </infinite-loading>-->
      </v-col>
      <!-- 右侧区域 -->
      <v-col
        md="3"
        cols="12"
        class="d-md-block d-none"
      >
        <div class="blog-wrapper">
          <!-- 博主信息 -->
          <v-card class="animated zoomIn blog-card mt-5">
            <div class="author-wrapper">
              <!-- 博主头像 -->
              <v-avatar size="110">
                <img
                  class="author-avatar"
                  :src="blogInfo.authorInfo.avatar"
                  alt="xqijun.top"
                >
              </v-avatar>
              <div style="font-size: 1.375rem">
                {{ blogInfo.authorInfo.nickName }}
              </div>
              <div style="font-size: 0.875rem;">
                {{ blogInfo.authorInfo.introduction }}
              </div>
            </div>
            <!-- 博客信息 -->
            <div class="blog-info-wrapper">
              <div class="blog-info-data">
                <div>
                  <div style="font-size: 0.875rem">
                    文章
                  </div>
                  <div style="font-size: 1.25rem">
                    {{ blogInfo.countNum.articleCount }}
                  </div>
                </div>
              </div>
              <div class="blog-info-data">
                <router-link to="/tags">
                  <div style="font-size: 0.875rem">
                    标签
                  </div>
                  <div style="font-size: 1.25rem">
                    {{ blogInfo.countNum.tagCount }}
                  </div>
                </router-link>
              </div>
              <div class="blog-info-data">
                <div>
                  <div style="font-size: 0.875rem">
                    留言
                  </div>
                  <div style="font-size: 1.25rem">
                    {{ blogInfo.countNum.msgCount }}
                  </div>
                </div>
              </div>
            </div>
            <!-- 收藏按钮 -->
            <a
              class="collection-btn"
              @click="tip = true"
            >
              <v-icon
                color="#fff"
                size="18"
                class="mr-1"
              >mdi-bookmark </v-icon>
              加入书签
            </a>
          </v-card>
          <!-- 社交账号信息展示 -->
          <v-card class="blog-card animated zoomIn mt-5 big">
            <div class="web-info-title">
              <i class="el-icon-s-custom" />
              账号
            </div>
            <div
              id="follow-us"
              ref="follow"
              class="guanzhu"
            >
              <ul>
                <li
                  v-if="isShowSocial(2)"
                  class="qq"
                >
                  <a
                    :href="
                      'tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=' +
                        blogInfo.authorInfo.qq
                    "
                    target="_blank"
                  >{{ blogInfo.authorInfo.qq }}</a>
                </li>
                <li
                  v-if="isShowSocial(1)"
                  class="email"
                >
                  <a href="javascript:void(0);">{{
                    blogInfo.authorInfo.email
                  }}</a>
                </li>
                <li
                  v-if="isShowSocial(3)"
                  class="github"
                >
                  <a
                    :href="blogInfo.authorInfo.githubAddress"
                    target="_blank"
                  >{{ blogInfo.authorInfo.githubAddress }}</a>
                </li>
                <li
                  v-if="isShowSocial(4)"
                  class="gitee"
                >
                  <a
                    :href="blogInfo.authorInfo.giteeAddress"
                    target="_blank"
                  >{{
                    blogInfo.authorInfo.giteeAddress
                  }}</a>
                </li>
              </ul>
            </div>
          </v-card>
          <!-- 标签云 -->
          <v-card class="blog-card animated zoomIn mt-5">
            <div class="web-info-title">
              标签云
            </div>
            <div class="web-info">
              <svg
                :width="width"
                :height="height"
                @mousemove="listener($event)"
              >
                <router-link
                  v-for="(tag, index) in tags"
                  :key="tag.id"
                  :to="'/tags/' + tag.id"
                >
                  <text
                    :x="tag.x"
                    :y="tag.y"
                    :font-size="20 * (600 / (600 - tag.z))"
                    :fill-opacity="(400 + tag.z) / 600"
                    :fill="colors[index]"
                  >
                    {{ tag.text }}
                  </text>
                </router-link>
              </svg>
            </div>
          </v-card>
          <!-- 网站信息 -->
          <v-card class="blog-card animated zoomIn mt-5">
            <div class="web-info-title">
              <v-icon size="18">
                mdi-chart-line
              </v-icon>
              网站资讯
            </div>
            <div class="web-info">
              <div style="padding:4px 0 0">
                运行时间:<span class="float-right">{{ time }}</span>
              </div>
              <div style="padding:4px 0 0">
                总访问量:<span class="float-right">
                  {{ blogInfo.basicInfo.browseNum }}
                </span>
              </div>
            </div>
          </v-card>
        </div>
      </v-col>
    </v-row>
    <!-- 提示消息 -->
    <v-snackbar
      v-model="tip"
      top
      color="#49b1f5"
      :timeout="2000"
    >
      按CTRL+D 键将本页加入书签
    </v-snackbar>


    <sho-notice ref="showNoticeRef" />
  </div>
</template>

<script>
import EasyTyper from 'easy-typer-js'
import {articleList} from '@/api'
import {metaInfo} from '@/config/BlogConstant'
import {markdownToHtml} from '@/utils/article'
import ShoNotice from '@/views/home/showNotice.vue'

export default {
  components: {ShoNotice},
  data() {
    return {
      width: 220, //svg宽度
      height: 230, //svg高度
      RADIUS: 100, //球的半径
      speedX: Math.PI / 360, //球一帧绕x轴旋转的角度
      speedY: Math.PI / 360, //球-帧绕y轴旋转的角度
      tags: [],
      colors: [], //存储颜色
      onlineCount: 0,
      heartBeat: null,
      formLabelWidth: '80px',
      articleLoadState: true,
      form: {
        type: 1,
        email: null,
        title: null,
        content: null,
        imgUrl: null
      },
      webSite: {},
      tip: false,
      totalPage: null,
      img: process.env.VUE_APP_IMG_API,
      time: '',
      obj: {
        output: '',
        isEnd: false,
        speed: 300,
        singleBack: false,
        sleep: 0,
        type: 'rollback',
        backSpeed: 40,
        sentencePause: true
      },
      articleList: [],
      params: {
        page: 1,
        size: 5,
        sort: 'a.isTop,a.id',
        order: 'desc,desc',
        connector: 'or',
        query: []
      },
      currentPageInfo: {
        pageKey: 'home',
        pageName: '首页',
        pageIntro: '',
        pageTitle: '',
        headerImgUrl: 'https://picture.xqijun.top/header2.jpg'
      },
      rules: {
        type: [{required: true, message: '必填字段', trigger: 'blur'}],
        email: [
          {required: true, message: '必填字段', trigger: 'blur'},
          {
            pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/,
            message: '填写正确的邮箱'
          }
        ],
        title: [{required: true, message: '必填字段', trigger: 'blur'}]
      },
      loading: true, //加载按钮动画
      loadBtnText: '加载更多...',
    }
  },
  computed: {
    CX() {
      return this.width / 2
    },
    CY() {
      return this.height / 2
    },
    isRight() {
      return function(index) {
        if (index % 2 === 0) {
          return 'article-cover left-radius'
        }
        return 'article-cover right-radius'
      }
    },
    blogInfo() {
      return this.$store.state.blogInfo
    },
    tagList() {
      return this.$store.state.blogInfo.articleTypes
    },
    isShowSocial() {
      return social => {
        // return this.$store.state.blogInfo.webSite.showList.indexOf(social) !== -1
        return true
      }
    },
    cover() {
      let cover = ''
      this.$store.state.blogInfo.pageInfo.forEach(item => {
        if (item.pageKey === 'home') {
          cover = item.headerImgUrl
          this.currentPageInfo = item
        }
      })
      return 'background: url(' + cover + ') center center / cover no-repeat'
    },
    noticeContent(){
      return markdownToHtml(this.$store.state.blogInfo.noticeInfo.content)
    }
  },
  created() {
    document.title = '小徐博客-分享踩坑日常，学习笔记'
    this.timer = setInterval(this.runTime, 1000)
    this.setLabelCloud()
  },
  mounted() {
    this.loadArticle()
  },
  metaInfo: metaInfo,
  methods: {
    setLabelCloud(){
      this.changeColors()
      //初始化标签位置
      let temTags = []
      this.$nextTick(()=>{
        setTimeout(()=>{
          let articleType = this.$store.state.blogInfo.articleTypes
          for (let i = 0; i < articleType.length; i++) {
            let tag = {}
            let k = -1 + (2 * (i + 1) - 1) / articleType.length
            let a = Math.acos(k)
            let b = a * Math.sqrt(articleType.length * Math.PI)
            tag.text = articleType[i].typeValue
            tag.x = this.CX + this.RADIUS * Math.sin(a) * Math.cos(b)
            tag.y = this.CY + this.RADIUS * Math.sin(a) * Math.sin(b)
            tag.z = this.RADIUS * Math.cos(a)
            tag.id = articleType[i].id
            temTags.push(tag)
          }
          this.tags = temTags
        },500)
      },300)
      setInterval(() => {
        this.rotateX(this.speedX)
        this.rotateY(this.speedY)
      }, 20)
    },
    showNotice(){
      this.$refs.showNoticeRef.open()
    },
    getTagName(id) {
      let typeValue = '--'
      this.tagList.forEach(e => {
        if (e.id === parseInt(id)) {
          typeValue = e.typeValue
        }
      })
      return typeValue
    },
    rotateX(angleX) {
      var cos = Math.cos(angleX)
      var sin = Math.sin(angleX)
      for (let tag of this.tags) {
        var y1 = (tag.y - this.CY) * cos - tag.z * sin + this.CY
        var z1 = tag.z * cos + (tag.y - this.CY) * sin
        tag.y = y1
        tag.z = z1
      }
    },
    rotateY(angleY) {
      var cos = Math.cos(angleY)
      var sin = Math.sin(angleY)
      for (let tag of this.tags) {
        var x1 = (tag.x - this.CX) * cos - tag.z * sin + this.CX
        var z1 = tag.z * cos + (tag.x - this.CX) * sin
        tag.x = x1
        tag.z = z1
      }
    },
    listener(event) {
      //响应鼠标移动
      var x = event.clientX - this.CX
      var y = event.clientY - this.CY
      this.speedX =
        x * 0.0001 > 0
          ? Math.min(this.RADIUS * 0.00002, x * 0.0001)
          : Math.max(-this.RADIUS * 0.00002, x * 0.0001)
      this.speedY =
        y * 0.0001 > 0
          ? Math.min(this.RADIUS * 0.00002, y * 0.0001)
          : Math.max(-this.RADIUS * 0.00002, y * 0.0001)
    },
    changeColors() {
      //随机变色
      for (var i = 0; i < 30; i++) {
        var r = Math.floor(Math.random() * 256)
        var g = Math.floor(Math.random() * 256)
        var b = Math.floor(Math.random() * 256)
        this.colors[i] = 'rgb(' + r + ',' + g + ',' + b + ')'
      }
    },

    initTyped(input, fn, hooks) {
      const obj = this.obj
      // eslint-disable-next-line no-unused-vars
      const typed = new EasyTyper(obj, input, fn, hooks)
    },
    scrollDown() {
      window.scrollTo({
        behavior: 'smooth',
        top: document.documentElement.clientHeight
      })
    },
    runTime() {
      var timeold = new Date().getTime() - new Date(1627747200000).getTime()
      var msPerDay = 24 * 60 * 60 * 1000
      var daysold = Math.floor(timeold / msPerDay)
      var str = ''
      var day = new Date()
      str += daysold + '天'
      str += day.getHours() + '时'
      str += day.getMinutes() + '分'
      str += day.getSeconds() + '秒'
      this.time = str
    },
    loadArticle() {
      const h = this.$createElement
      if (!this.articleLoadState){
        this.$notify({
          title: '提示',
          message: h('i', {style: 'color: teal'}, '没有更多了')
        })
        return
      }
      this.loading = true
      articleList(this.params).then(res => {
        let data = res.data.data.rows
        if (data.length) {
          // 去除markdown标签
          data.forEach(item => {
            this.articleList.push(item)
          })
          this.articleLoadState = true
          this.params.page++
        } else {
          this.articleLoadState = false
          this.$notify({
            title: '提示',
            message: h('i', {style: 'color: teal'}, '没有更多了')
          })
        }
        this.loading = false
      })
    }
  }
}
</script>

<style lang="stylus">
.typed-cursor{
  opacity: 1;
  animation: blink 0.7s infinite
}

@keyframes blink {
  0% {
    opacity: 1
  }
  50% {
    opacity: 0
  }

  100% {
    opacity: 1
  }
}

</style>

<style scoped>
.home-banner {
  position: absolute;
  top: -60px;
  left: 0;
  right: 0;
  height: 100vh;
  background-attachment: fixed;
  text-align: center;
  color: #fff !important;
  animation: header-effect 1s;
}

.banner-container {
  margin-top: 43vh;
  line-height: 1.5;
  color: #eee;
}

.blog-contact a {
  color: #fff !important;
}

.card-info-social {
  line-height: 40px;
  text-align: center;
  margin: 6px 0 -6px;
}

.card-info-social a {
  font-size: 1.5rem;
}

.left-radius {
  border-radius: 8px 0 0 8px !important;
  order: 0;
}

.right-radius {
  border-radius: 0 8px 8px 0 !important;
  order: 1;
}

.article-wrapper {
  font-size: 14px;
}

.blog-title,.blog-intro{
  color: rgb(255, 255, 255);
  text-shadow: rgb(28, 31, 33) -3px 2px 6px;
}

@media (min-width: 760px) {
  .blog-title {
    font-size: 2.5rem;
  }

  .blog-intro {
    font-size: 1.5rem;
  }

  .blog-contact {
    display: none;
  }

  .home-container {
    max-width: 1200px;
    margin: calc(100vh - 48px) auto 28px auto;
    padding: 0 5px;
  }

  .article-card {
    display: flex;
    align-items: center;
    height: 280px;
    width: 100%;
    margin-top: 20px;
  }

  .article-cover {
    overflow: hidden;
    height: 100%;
    width: 45%;
  }

  .on-hover {
    transition: all 0.6s;
  }

  .article-card:hover .on-hover {
    transform: scale(1.1);
  }

  .article-wrapper {
    padding: 0 2.5rem;
    width: 55%;
  }

  .article-wrapper a {
    font-size: 1.5rem;
    transition: all 0.3s;
  }
}

@media (max-width: 759px) {
  .blog-title {
    font-size: 26px;
  }

  .blog-contact {
    font-size: 1.25rem;
    line-height: 2;
  }

  .home-container {
    width: 100%;
    margin: calc(100vh - 66px) auto 0 auto;
  }

  .article-card {
    margin-top: 1rem;
  }

  .article-cover {
    border-radius: 8px 8px 0 0 !important;
    height: 230px !important;
    width: 100%;
  }

  .article-cover div {
    border-radius: 8px 8px 0 0 !important;
  }

  .article-wrapper {
    padding: 1.25rem 1.25rem 1.875rem;
  }

  .article-wrapper a {
    font-size: 1.25rem;
    transition: all 0.3s;
  }
}

.scroll-down {
  cursor: pointer;
  position: absolute;
  bottom: 0;
  width: 100%;
}

.scroll-down i {
  font-size: 2rem;
}

.article-wrapper a:hover {
  color: #8e8cd8;
}

.article-info {
  font-size: 95%;
  color: #858585;
  line-height: 2;
  margin: 0.375rem 0;
}

.article-info a {
  font-size: 95%;
  color: #858585 !important;
}

.article-content {
  line-height: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.blog-wrapper {
  position: sticky;
  top: 10px;
}

.blog-card {
  line-height: 2;
  padding: 1.25rem 1.5rem;
}

.author-wrapper {
  text-align: center;
}

.blog-info-wrapper {
  display: flex;
  justify-self: center;
  padding: 0.875rem 0;
}

.blog-info-data {
  flex: 1;
  text-align: center;
}

.blog-info-data a {
  text-decoration: none;
}

.collection-btn {
  text-align: center;
  z-index: 1;
  font-size: 14px;
  position: relative;
  display: block;
  background-color: #49b1f5;
  color: #fff !important;
  height: 32px;
  line-height: 32px;
  transition-duration: 1s;
  transition-property: color;
}

.collection-btn:before {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  background: #ff7242;
  content: "";
  transition-timing-function: ease-out;
  transition-duration: 0.5s;
  transition-property: transform;
  transform: scaleX(0);
  transform-origin: 0 50%;
}

.collection-btn:hover:before {
  transition-timing-function: cubic-bezier(0.45, 1.64, 0.47, 0.66);
  transform: scaleX(1);
}

.author-avatar {
  transition: all 0.5s;
}

.author-avatar:hover {
  transform: rotate(360deg);
}

.web-info {
  padding: 0.25rem;
  font-size: 0.875rem;
}

.scroll-down-effects {
  color: #eee !important;
  text-align: center;
  text-shadow: 0.1rem 0.1rem 0.2rem rgba(0, 0, 0, 0.15);
  line-height: 1.5;
  display: inline-block;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  animation: scroll-down-effect 1.5s infinite;
}

@keyframes scroll-down-effect {
  0% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
  50% {
    top: -16px;
    opacity: 1;
    filter: none;
  }
  100% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
}

.big i {
  color: #f00;
  animation: big 0.8s linear infinite;
}

@keyframes big {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
}

.guanzhu {
  margin-top: 10px;
}

.guanzhu .qqGroup {
  background: url()
    no-repeat;
  background-size: 22px;
  background-position-y: 47%;
}

.guanzhu .qq {
  background: url()
    no-repeat 0 -98px;
}

.guanzhu .email {
  background: url()
    no-repeat 0 -150px;
}

.guanzhu .github {
  background: url(github-fill.png) no-repeat;
  background-size: 25px;
  background-position-y: 47%;
}

.guanzhu .gitee {
  background: url()
    no-repeat;
  background-size: 25px;
  background-position-y: 47%;
}

.guanzhu ul li {
  font-size: 12px;
  margin-bottom: 10px;
  background: #fff;
  color: #525252;
  line-height: 40px;
  padding: 0 0 0 34px;
  border: 1px solid #ddd;
  border-radius: 2px;
  position: relative;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
</style>
